package com.zzh.media.recorder;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zzh.lib.NativeLib;
import com.zzh.lib.player.FMediaPlayer;
import com.zzh.lib.recorder.AudioFormat;
import com.zzh.lib.recorder.HMediaRecorder;
import com.zzh.lib.recorder.HRecorder;
import com.zzh.lib.recorder.base.utils.StaticFinalValues;
import com.zzh.lib.recorder.mp3.Mp3Recorder;
import com.zzh.lib.recorder.whole.createVideoByVoice.localEdit.LocalVideoActivity;
import com.zzh.lib.recorder.whole.pickvideo.VideoPickActivity;
import com.zzh.lib.recorder.whole.pickvideo.beans.VideoFile;
import com.zzh.lib.recorder.whole.record.RecorderActivity;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import static com.zzh.lib.recorder.base.utils.StaticFinalValues.REQUEST_CODE_PICK_VIDEO;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private final HMediaRecorder mRecorder = new HMediaRecorder();
    private Mp3Recorder mMp3Recorder = new Mp3Recorder();
    private final FMediaPlayer mPlayer = new FMediaPlayer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((TextView) findViewById(R.id.tv_lame_version)).setText(new NativeLib().stringFromJNI());
        initRecorder();
        initMp3Recorder();
        initPlayer();
    }

    private void initMp3Recorder() {

    }

    private void initRecorder() {
        mRecorder.init(this);
    }

    private void initPlayer() {
        mPlayer.init();
        mPlayer.addOnStateChangeCallback(new FMediaPlayer.OnStateChangeCallback() {
            @Override
            public void onStateChanged(FMediaPlayer player, FMediaPlayer.State oldState, FMediaPlayer.State newState) {
                Log.i(TAG, "Player onStateChanged:" + newState);
            }
        });
        mPlayer.setOnExceptionCallback(new FMediaPlayer.OnExceptionCallback() {
            @Override
            public void onException(Exception e) {
                Log.i(TAG, "Player onException:" + e);
            }
        });
    }

    /**
     * 开始录音
     */
    public void onClickStartRecord(View view) {
        mPlayer.reset();
        //mRecorder.start(new File(getExternalCacheDir(), "record.aac"));
        //mMp3Recorder.start(new File(getExternalCacheDir(), "record.mp3"));
        try {
            HRecorder.getInstance().setAudioFormat(AudioFormat.AAC).start();
        } catch (Exception e) {
            Log.e("---", e.getMessage());
        }
    }

    /**
     * 停止录音
     */
    public void onClickStopRecord(View view) {
        //mRecorder.stop();
//        mMp3Recorder.stop();
        HRecorder.getInstance().stop();
    }

    /**
     * 开始播放
     */
    public void onClickStartPlay(View view) {
        mPlayer.start();
    }

    /**
     * 停止播放
     */
    public void onClickStopPlay(View view) {
        mPlayer.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRecorder.release();
        mPlayer.release();
    }

    public void onClickVideo(View view) {
        Intent intent = new Intent(this, RecorderActivity.class);
        startActivity(intent);
    }

    public void onClick1(View view) {

        startActivity(new Intent(this, ChangeVoiceActivity.class));

    }

    public void onClick2(View view) {
        startActivity(new Intent(this, AudioRecordActivity.class));
    }

    public void onClickLocalVideo(View view) {
        startActivityForResult(new Intent(this, VideoPickActivity.class), REQUEST_CODE_PICK_VIDEO);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void onClick3(View view) {
        if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
        } else {
            requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.MANAGE_EXTERNAL_STORAGE}, 1000);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_PICK_VIDEO:
                if (resultCode == RESULT_OK) {
                    ArrayList<VideoFile> list = data.getParcelableArrayListExtra(StaticFinalValues.RESULT_PICK_VIDEO);
                    String videoFileName = "";
                    for (VideoFile file : list) {
                        videoFileName = file.getPath();
                    }


                    //这一段用来判断视频时间的
                    try {
                        MediaPlayer player = new MediaPlayer();
                        player.setDataSource(videoFileName);
                        player.prepare();
                        int duration = player.getDuration();
                        player.release();
                        int s = duration / 1000;
                        int hour = s / 3600;
                        int minute = s % 3600 / 60;
                        int second = s % 60;
                        Log.e(TAG, "视频文件长度,分钟: " + minute + "视频有" + s + "秒");
                        if (s >= 120) {
                            Toast.makeText(this, "视频剪辑不能超过2分钟", Toast.LENGTH_LONG).show();
                            return;
                        } else if (s < 5) {
                            Toast.makeText(this, "视频剪辑不能少于5秒", Toast.LENGTH_LONG).show();
                            return;
                        }else{
                            Intent intent = new Intent(this, LocalVideoActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString(StaticFinalValues.VIDEOFILEPATH, videoFileName);
                            bundle.putInt(StaticFinalValues.MISNOTCOMELOCAL, 0);
                            intent.putExtra(StaticFinalValues.BUNDLE, bundle);
                            startActivity(intent);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }
}
